//
//  LFURlRequest.m
//  CoreDataForHXClient
//
//  Created by Apple on 14-6-27.
//  Copyright (c) 2014年 Lemon. All rights reserved.
//

#import "LFURlRequest.h"
#import "FBEncryptorDES.h"

static NSString *const app_id_value = @"Mobile";
static NSString *const certi_id_value = @"2356221002";
static NSString *const token = @"m9#=r_sgpy=s@@-cghz&amp;pq2wv%k2+g(bly3f5wy-_pp7iz^!cf";





//#ifndef _LFURLRequest_Define__
//#define _LFURLRequest_Define__
//
//#define HX_SERVE @"http://betone.308308.com/betone/api/"
//#define HX_SP_APP_ID   @"app_id"                                                //应用ID (系统标识)------值为:IPhone
//#define HX_SP_CERTI_ID @"certi_id"                                              //应用认证ID -------值为:2356221002
//#define HX_SP_METHOD   @"method"                                                //接口方法名称
//#define HX_SP_SIGN     @"sign"                                                  //API输入参数签名结果
//#define HX_SP_DATE     @"date"                                                  //时间戳(YYYY-MM-DD HH24:Mi:SS)  请求时间
//#define HX_SP_DATA     @"data"                                                  //提交参数值JSON对象字符串
//
//#endif

@implementation LFURlRequest

+ (NSURLRequest *)getUrlRequestWithMethod:(NSString *)strMethod andParams:(NSDictionary *)dicParams
{
    NSString *strParamsJson = @"";
    if (dicParams)
    {
        NSError *jsonError;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicParams
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:&jsonError];
        if (!jsonError)
        {
            strParamsJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        } else
        {
            return nil;
        }
    } else
    {
        return nil;
    }
    
    NSDate *requestDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *strRequestDate = [dateFormatter stringFromDate:requestDate];
    
    /**
     *  sign
     */
    
    NSString *strSign = [LFURlRequest buildSign:app_id_value and:certi_id_value and:strParamsJson and:strRequestDate and:strMethod];
    
    NSDictionary *postValue = [NSDictionary dictionaryWithObjectsAndKeys:
                               app_id_value, HX_SP_APP_ID,
                               certi_id_value, HX_SP_CERTI_ID,
                               strMethod, HX_SP_METHOD,
                               strSign, HX_SP_SIGN,
                               strRequestDate, HX_SP_DATE,
                               strParamsJson, HX_SP_DATA, nil];
    
    /**
     *  postUrl
     */
    NSString *strPostValue = [LFURlRequest createPostURL:postValue];
    
    NSMutableData *postData = [[strPostValue dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
    NSURL *url = [NSURL URLWithString:HX_SERVE];
    
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
    [urlRequest setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-type"];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:postData];
    //urlRequest.cachePolicy = NSURLRequestReturnCacheDataDontLoad;
    [urlRequest setTimeoutInterval:30.f];
    return urlRequest;
}

/**
 *  创建post方式的参数url
 *
 *  @param bodyDictionary 传入参数
 *
 *  @return 返回post参数url
 */
+ (NSString *)createPostURL:(NSDictionary *)bodyDictionary
{
    NSString *postString = @"";
    for(NSString *key in [bodyDictionary allKeys])
    {
        NSString *value=[bodyDictionary objectForKey:key];
        postString=[postString stringByAppendingFormat:@"%@=%@&",key,value];
    }
    if([postString length]>1)
    {
        postString=[postString substringToIndex:[postString length]-1];
    }
    
    return postString;
}

/**
 *  构造sign
 *
 *  @param app_id   用户id
 *  @param certi_id 应用认证ID
 *  @param data     参数值
 *  @param date     请求时间
 *  @param method   请求方法
 *
 *  @return sign
 */
+ (NSString *)buildSign:(NSString *)app_id and:(NSString *)certi_id and:(NSString *)data and:(NSString *)date and:(NSString *)method
{
    NSString *str = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@",
                     HX_SP_APP_ID, app_id,
                     HX_SP_CERTI_ID, certi_id,
                     HX_SP_DATA, data,
                     HX_SP_DATE, date,
                     HX_SP_METHOD, method];
    //第一次md5
    str = [[FBEncryptorDES md5:str] uppercaseString];
    //拼接token
    str = [str stringByAppendingString:token];
    //结果
    str = [[FBEncryptorDES md5:str] uppercaseString];
    return str;
}

@end
